package 笔试真题;

import java.util.Scanner;

public class Tengxun23 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String s = scanner.next();
        int numOfZero = 0;
        char[] chars = s.toCharArray();
        for (char c : s.toCharArray()) {
            if(c == '0'){
                numOfZero++;
            }
        }
        int need = Math.max(numOfZero,n - numOfZero);
        int ans  = (1+need)*need/2;
        if(numOfZero > n/2){
            int a = getNumStart('1',chars);
            int b = getNumEnd('1',chars);
            ans += (a+1)*a/2;
            ans += (b+1)*b/2;
        }else{
            int a = getNumStart('0',chars);
            int b = getNumEnd('0',chars);
            ans += (a+1)*a/2;
            ans += (b+1)*b/2;
        }
        System.out.println(ans);

    }
    public static int getNumStart(char c,char[] chars){
        int ans = 0;
        for (char aChar : chars) {
            if (c == aChar) {
                ans++;
            } else {
                break;
            }
        }
        return ans;
    }

    public static int getNumEnd(char c,char[] chars){
        int ans = 0;
        for (int i = chars.length-1; i >= 0; i--) {
            if(chars[i] == c){
                ans ++;
            }else {
                break;
            }
        }
        return ans;
    }
}
